import request from '../../common/request.js';

const state = {
  userInfo: {},
  userId: ''
}
const mutations = {
	SET_USERINFO: (state, userInfo) => {
		state.userInfo = userInfo
	},
	SET_USERID: (state, userId) => {
		state.userId = userId
	},
}
const actions = {
	async login({commit}, payload) {
		const res = await request('/login/cellphone', payload);
		var userInfo = {
			nickname: res.profile.nickname,
			uid: res.profile.userId,
			avatarUrl: res.profile.avatarUrl
		};
		commit('SET_USERINFO', userInfo);
		commit('SET_USERID', userInfo.uid);
		uni.redirectTo({
			url: '/pages/index/main'
		});
	},
	async loginout({commit}) {
		const res = await request('/logout');
		commit('SET_USERINFO', {});
		commit('SET_USERID', '');
		uni.redirectTo({
			url: '/pages/login/index',
		});
	},
}

export default {
	namespaced: true,
	state,
	mutations,
	actions,
}